/*
 * @Author: your name
 * @Date: 2022-03-29 22:39:39
 * @LastEditTime: 2022-04-21 17:17:48
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \blog-api\src\user\entities\user.entity.ts
 */

import { Column, Entity, PrimaryGeneratedColumn, BeforeInsert } from 'typeorm';
import { hashPassword, isOk } from '../../../utils/bcrypt'

@Entity('user')
export class User {
    @PrimaryGeneratedColumn()
    user_id: number;

    @Column({ 
        type: 'varchar',
        length: 50,
        name: 'username',
        comment: '用户名',
    })
    username: string; // 用户名

    @Column({ 
        type: 'varchar',
        length: 20,
        name: 'password',
        comment: '密码',
    })
    password: string;  // 密码

    @Column({
        type: 'varchar',
        length: 320,
        unique: true,
        comment: '邮箱'
    })
    email: string;
    
    @Column({
        comment: '昵称'
    })
    nickname: string;

    @Column({ 
        type: 'text',
        name: 'portrait',
        comment: '头像',
    })
    portrait: string;   //头像

    @Column({ 
        type: 'varchar',
        length: 11,
        name: 'user_tel',
        comment: '手机号',
    })
    user_tel: string; // 手机号

    @Column({ 
        type: 'int',
        name: 'article_num',
        comment: '文章数量',
    })
    article_num: number; // 文章数量
    
    @Column({
        name: 'create_time',
        type: 'timestamp',
        comment: '创建时间',
        default: () => 'CURRENT_TIMESTAMP',
    })
    createTime: Date;

    @Column({
        name: 'update_time',
        type: 'timestamp',
        comment: '更新时间',
        default: () => 'CURRENT_TIMESTAMP',
        onUpdate: 'CURRENT_TIMESTAMP',
    })
    updateTime: Date;

    // @BeforeInsert()
    // async encryptPwd() {
    //     this.password = await hashPassword(this.password, 10);
    // }
}
